﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.Mappings;

namespace Apollo.Modules.Salary
{
    [NHibernateEntity(Table = "SAPExportMappings")]
    public class SAPExportMapping : IHistory<SAPExportMapping>, ITrackable
    {
        public SAPExportMapping() {
            this.LastDayOfMonth = DateTime.Now.ToString("ddMMyyyy");
        }

        [PrimaryKey]
        public virtual int Id { get; set; }

        [Property]
        public virtual string Name { get; set; }

        [Property]
        public virtual SAPMappingType Type { get; set; }

        [BelongsTo(Column = "ID_TransactionDefinition")]
        public virtual TransactionDefinition TransactionDefinition { get; set; }

        [BelongsTo(Column = "ID_EmployeeComponentDefinition")]
        public virtual EmployeeComponentDefinition EmployeeComponentDefinition { get; set; }

        [Property]
        public virtual int NEWBS { get; set; }

        [Property]
        public virtual string NEWKO { get; set; }

        [Property]
        public virtual string NEWUM { get; set; }

        [Property]
        public virtual string XNEGP { get; set; }
        


        [BelongsTo(Column = "ID_BelonsTo_SAPExportMapping")]
        public virtual SAPExportMapping BelongsTo { get; set; }

        [Property]
        public virtual DateTime EffectiveDate { get; set; }

        [Property]
        public virtual DateTime LearnDate { get; set; }

        [Property]
        public virtual State State { get; set; }



        [Property]
        public virtual DateTime InsertDate { get; set; }

        [Property]
        public virtual string InsertBy { get; set; }

        [Property]
        public virtual DateTime? ModifiedDate { get; set; }

        [Property]
        public virtual string ModifiedBy { get; set; }


        #region CSV Methods & Properties

        public virtual string AccountSufixPF { get; set; }
        public virtual string AccountSufixPJ { get; set; }
        public virtual string LastDayOfMonth { get; set; }
        public virtual bool IsGeneralLedger { get { return this.NEWBS == 40 || this.NEWBS == 50; } }
        
        public static string GetCSVHeader() {
            return "BUKRS;BUDAT;BLDAT;BLART;WAERS"
                + ";XBLNR;NEWBS;NEWUM;NEWKO;WRBTR"
                + ";VALUT;KOSTL;GSBER;SGTXT;ZUONR"
                + ";XNEGP;ZFBDT";
        }

        public virtual string GetCSVRow(double grossValue, string employeeCode, EmployeeType employeeType, string costCenter, string uniqueID)
        {
            if (grossValue == 0)
                return null;

            // calculate fields
            string actualNEWKO = this.NEWKO.Replace("AAAAA", employeeCode)
                .Replace("[EMPLOYEE_CODE]", employeeCode);
            actualNEWKO = actualNEWKO.Replace("XXX", (employeeType == EmployeeType.Agent ? this.AccountSufixPF : this.AccountSufixPJ));
            actualNEWKO = actualNEWKO.Replace("[ACCOUNT_SUFIX]", (employeeType == EmployeeType.Agent ? this.AccountSufixPF : this.AccountSufixPJ));
            string actualZFBDT = string.IsNullOrEmpty(this.NEWUM) ? "" : this.LastDayOfMonth;
            string actualKOSTL = this.IsGeneralLedger ? costCenter : "";
            
            // actual CSV line
            return "OAVL;" + this.LastDayOfMonth + ";" + this.LastDayOfMonth + ";SL;RON;"
                    + uniqueID + ";" + this.NEWBS.ToString() + ";" + this.NEWUM + ";"
                    + actualNEWKO + ";" + grossValue.ToString() + ";;" + actualKOSTL
                    + ";;" + this.Name + ";;" + this.XNEGP + ";" + actualZFBDT;

        }

        public virtual string GetCSVRow(double grossValue, string employeeCode, EmployeeType employeeType, string costCenter, string uniqueID, bool includeNewLine) {
            if (!includeNewLine)
                return this.GetCSVRow(grossValue, employeeCode, employeeType, costCenter, uniqueID);
            else {
                string row = this.GetCSVRow(grossValue, employeeCode, employeeType, costCenter, uniqueID);

                if (string.IsNullOrEmpty(row))
                    return null;
                else
                    return "\n" + row;
            }
        }

        #endregion
    }

    public enum SAPMappingType { 
        BaseIncome = 0,
        Transaction = 1,
        GrossIncome = 2,
        NetIncome = 3,
        Component = 4,
        PaymentTotal = 5,
    }
}
